package com.code2roc.jarmanage.util;

import com.code2roc.jarmanage.db.ProxyDB;
import com.code2roc.jarmanage.model.ProxyDO;
import com.code2roc.jarmanage.proxy.ProxyServlet;
import com.code2roc.jarmanage.servlet.command.*;
import com.code2roc.jarmanage.servlet.config.*;
import com.code2roc.jarmanage.servlet.depoly.*;
import com.code2roc.jarmanage.servlet.depolyversion.*;
import com.code2roc.jarmanage.servlet.pfmanage.FileDetailServlet;
import com.code2roc.jarmanage.servlet.pfmanage.FileDownLoadServlet;
import com.code2roc.jarmanage.servlet.pfmanage.FileListSerlvet;
import com.code2roc.jarmanage.servlet.firewall.FireWallDeleteServlet;
import com.code2roc.jarmanage.servlet.firewall.FireWallInsertServlet;
import com.code2roc.jarmanage.servlet.firewall.FireWallListServlet;
import com.code2roc.jarmanage.servlet.group.*;
import com.code2roc.jarmanage.servlet.machine.MachineDetailServlet;
import com.code2roc.jarmanage.servlet.machine.MachineRefreshServelt;
import com.code2roc.jarmanage.servlet.nginx.*;
import com.code2roc.jarmanage.servlet.project.*;
import com.code2roc.jarmanage.servlet.proxy.*;
import com.code2roc.jarmanage.servlet.redis.*;
import com.code2roc.jarmanage.servlet.service.ServiceListServlet;
import com.code2roc.jarmanage.servlet.service.StartServiceServlet;
import com.code2roc.jarmanage.servlet.service.StopServiceServlet;
import com.code2roc.jarmanage.servlet.war.*;
import org.apache.catalina.Container;
import org.apache.catalina.Wrapper;
import org.apache.catalina.core.StandardContext;
import org.apache.catalina.startup.Tomcat;

import java.util.List;

public class RouteUtil {
    private static Tomcat tomcatInstance = null;
    private static StandardContext ctxInstance = null;

    public static void initMapping(Tomcat tomcat, StandardContext ctx) {
        tomcatInstance = tomcat;
        ctxInstance = ctx;
        ///region project
        tomcat.addServlet(ctx, "InsertServlet", new InsertServlet());
        ctx.addServletMappingDecoded("/api/project/insert", "InsertServlet");

        tomcat.addServlet(ctx, "DeleteServlet", new DeleteServlet());
        ctx.addServletMappingDecoded("/api/project/delete", "DeleteServlet");

        tomcat.addServlet(ctx, "UpdateServlet", new UpdateServlet());
        ctx.addServletMappingDecoded("/api/project/update", "UpdateServlet");

        tomcat.addServlet(ctx, "DetailServlet", new DetailServlet());
        ctx.addServletMappingDecoded("/api/project/detail", "DetailServlet");

        tomcat.addServlet(ctx, "ListServlet", new ListServlet());
        ctx.addServletMappingDecoded("/api/project/list", "ListServlet");
        ///endregion

        ///region war
        tomcat.addServlet(ctx, "WarInsertSerlvet", new WarInsertSerlvet());
        ctx.addServletMappingDecoded("/api/war/insert", "WarInsertSerlvet");

        tomcat.addServlet(ctx, "WarDeleteSerlvet", new WarDeleteSerlvet());
        ctx.addServletMappingDecoded("/api/war/delete", "WarDeleteSerlvet");

        tomcat.addServlet(ctx, "WarUpdateSerlvet", new WarUpdateSerlvet());
        ctx.addServletMappingDecoded("/api/war/update", "WarUpdateSerlvet");

        tomcat.addServlet(ctx, "WarDetailSerlvet", new WarDetailSerlvet());
        ctx.addServletMappingDecoded("/api/war/detail", "WarDetailSerlvet");

        tomcat.addServlet(ctx, "WarListSerlvet", new WarListSerlvet());
        ctx.addServletMappingDecoded("/api/war/list", "WarListSerlvet");
        ///endregion

        ///region depoly
        tomcat.addServlet(ctx, "InstallServlet", new InstallServlet());
        ctx.addServletMappingDecoded("/api/depoly/install", "InstallServlet");

        tomcat.addServlet(ctx, "StartServlet", new StartServlet());
        ctx.addServletMappingDecoded("/api/depoly/start", "StartServlet");

        tomcat.addServlet(ctx, "StopServlet", new StopServlet());
        ctx.addServletMappingDecoded("/api/depoly/stop", "StopServlet");

        tomcat.addServlet(ctx, "UnInstallServlet", new UnInstallServlet());
        ctx.addServletMappingDecoded("/api/depoly/uninstall", "UnInstallServlet");

        tomcat.addServlet(ctx, "GetBackUpListServlet", new GetBackUpListServlet());
        ctx.addServletMappingDecoded("/api/depoly/getBackUpList", "GetBackUpListServlet");

        tomcat.addServlet(ctx, "RecoverBackUpServlet", new RecoverBackUpServlet());
        ctx.addServletMappingDecoded("/api/depoly/recoverBackUp", "RecoverBackUpServlet");

        tomcat.addServlet(ctx, "GetRunningLogServlet", new GetRunningLogServlet());
        ctx.addServletMappingDecoded("/api/depoly/getRunningLog", "GetRunningLogServlet");

        tomcat.addServlet(ctx, "CheckLockServlet", new CheckLockServlet());
        ctx.addServletMappingDecoded("/api/depoly/checkLock", "CheckLockServlet");
        ///endregion

        ///region machine
        tomcat.addServlet(ctx, "MachineDetailServlet", new MachineDetailServlet());
        ctx.addServletMappingDecoded("/api/machine/detail", "MachineDetailServlet");

        tomcat.addServlet(ctx, "MachineRefreshServelt", new MachineRefreshServelt());
        ctx.addServletMappingDecoded("/api/machine/refresh", "MachineRefreshServelt");
        ///endregion

        ///region redis
        tomcat.addServlet(ctx, "GetDataBaseInfoServlet", new GetDataBaseInfoServlet());
        ctx.addServletMappingDecoded("/api/redis/getDataBaseInfo", "GetDataBaseInfoServlet");

        tomcat.addServlet(ctx, "GetDataBaseKeyListServlet", new GetDataBaseKeyListServlet());
        ctx.addServletMappingDecoded("/api/redis/getDataBaseKeyList", "GetDataBaseKeyListServlet");

        tomcat.addServlet(ctx, "AddKeyServlet", new AddKeyServlet());
        ctx.addServletMappingDecoded("/api/redis/addKey", "AddKeyServlet");

        tomcat.addServlet(ctx, "GetValueDetailServlet", new GetValueDetailServlet());
        ctx.addServletMappingDecoded("/api/redis/getValueDetail", "GetValueDetailServlet");

        tomcat.addServlet(ctx, "UpdateStringValueServlet", new UpdateStringValueServlet());
        ctx.addServletMappingDecoded("/api/redis/updateStringValue", "UpdateStringValueServlet");

        tomcat.addServlet(ctx, "AddRowServlet", new AddRowServlet());
        ctx.addServletMappingDecoded("/api/redis/addRow", "AddRowServlet");

        tomcat.addServlet(ctx, "DeleteRowServlet", new DeleteRowServlet());
        ctx.addServletMappingDecoded("/api/redis/deleteRow", "DeleteRowServlet");

        tomcat.addServlet(ctx, "UpdateRowServlet", new UpdateRowServlet());
        ctx.addServletMappingDecoded("/api/redis/updateRow", "UpdateRowServlet");

        tomcat.addServlet(ctx, "DeleteKeyServlet", new DeleteKeyServlet());
        ctx.addServletMappingDecoded("/api/redis/deleteKey", "DeleteKeyServlet");

        tomcat.addServlet(ctx, "UpdateExpireServlet", new UpdateExpireServlet());
        ctx.addServletMappingDecoded("/api/redis/updateExpire", "UpdateExpireServlet");

        tomcat.addServlet(ctx, "FlushDBServlet", new FlushDBServlet());
        ctx.addServletMappingDecoded("/api/redis/flushDB", "FlushDBServlet");
        ///endregion

        ///region proxy
        tomcat.addServlet(ctx, "ProxyInsertServlet", new ProxyInsertServlet());
        ctx.addServletMappingDecoded("/api/proxy/insert", "ProxyInsertServlet");

        tomcat.addServlet(ctx, "ProxyDeleteServlet", new ProxyDeleteServlet());
        ctx.addServletMappingDecoded("/api/proxy/delete", "ProxyDeleteServlet");

        tomcat.addServlet(ctx, "ProxyUpdateServlet", new ProxyUpdateServlet());
        ctx.addServletMappingDecoded("/api/proxy/update", "ProxyUpdateServlet");

        tomcat.addServlet(ctx, "ProxyDetailServlet", new ProxyDetailServlet());
        ctx.addServletMappingDecoded("/api/proxy/detail", "ProxyDetailServlet");

        tomcat.addServlet(ctx, "ProxyListServlet", new ProxyListServlet());
        ctx.addServletMappingDecoded("/api/proxy/list", "ProxyListServlet");
        ///endregion

        ///region proxy resigt
        List<ProxyDO> proxyDOList = ProxyDB.selectList("1=1");
        for (ProxyDO entity : proxyDOList) {
            addProxy(entity);
        }
        ///endregion

        ///region service
        tomcat.addServlet(ctx, "ServiceListServlet", new ServiceListServlet());
        ctx.addServletMappingDecoded("/api/service/list", "ServiceListServlet");

        tomcat.addServlet(ctx, "StartServiceServlet", new StartServiceServlet());
        ctx.addServletMappingDecoded("/api/service/start", "StartServiceServlet");

        tomcat.addServlet(ctx, "StopServiceServlet", new StopServiceServlet());
        ctx.addServletMappingDecoded("/api/service/stop", "StopServiceServlet");
        ///endregion

        ///region config
        tomcat.addServlet(ctx, "ConfigListServlet", new ConfigListServlet());
        ctx.addServletMappingDecoded("/api/config/list", "ConfigListServlet");

        tomcat.addServlet(ctx, "ConfigDeleteSerlvet", new ConfigDeleteSerlvet());
        ctx.addServletMappingDecoded("/api/config/delete", "ConfigDeleteSerlvet");

        tomcat.addServlet(ctx, "ConfigDetailServlet", new ConfigDetailServlet());
        ctx.addServletMappingDecoded("/api/config/detail", "ConfigDetailServlet");

        tomcat.addServlet(ctx, "ConfigUpdateSerlvet", new ConfigUpdateSerlvet());
        ctx.addServletMappingDecoded("/api/config/update", "ConfigUpdateSerlvet");

        tomcat.addServlet(ctx, "DownLoadConfigSerlvet", new DownLoadConfigSerlvet());
        ctx.addServletMappingDecoded("/api/config/download", "DownLoadConfigSerlvet");

        tomcat.addServlet(ctx, "SystemConfigSerlvet", new SystemConfigSerlvet());
        ctx.addServletMappingDecoded("/api/config/system", "SystemConfigSerlvet");
        ///endregion

        ///region nginx
        tomcat.addServlet(ctx, "NginxHomeDetailServlet", new NginxHomeDetailServlet());
        ctx.addServletMappingDecoded("/api/nginx/home/detail", "NginxHomeDetailServlet");

        tomcat.addServlet(ctx, "NginxHomeSaveServlet", new NginxHomeSaveServlet());
        ctx.addServletMappingDecoded("/api/nginx/home/save", "NginxHomeSaveServlet");

        tomcat.addServlet(ctx, "NginxConfigDetailServlet", new NginxConfigDetailServlet());
        ctx.addServletMappingDecoded("/api/nginx/config/detail", "NginxConfigDetailServlet");

        tomcat.addServlet(ctx, "NginxConfigUpdateServlet", new NginxConfigUpdateServlet());
        ctx.addServletMappingDecoded("/api/nginx/config/update", "NginxConfigUpdateServlet");

        tomcat.addServlet(ctx, "NginxFileListServlet", new NginxFileListServlet());
        ctx.addServletMappingDecoded("/api/nginx/file/list", "NginxFileListServlet");

        tomcat.addServlet(ctx, "NginxFileDeleteSerlvet", new NginxFileDeleteSerlvet());
        ctx.addServletMappingDecoded("/api/nginx/file/delete", "NginxFileDeleteSerlvet");

        tomcat.addServlet(ctx, "NginxFileDownLoadSerlvet", new NginxFileDownLoadSerlvet());
        ctx.addServletMappingDecoded("/api/nginx/file/download", "NginxFileDownLoadSerlvet");

        tomcat.addServlet(ctx, "NginxFileDetailServlet", new NginxFileDetailServlet());
        ctx.addServletMappingDecoded("/api/nginx/file/detail", "NginxFileDetailServlet");

        tomcat.addServlet(ctx, "NginxFileUpdateServlet", new NginxFileUpdateServlet());
        ctx.addServletMappingDecoded("/api/nginx/file/update", "NginxFileUpdateServlet");

        tomcat.addServlet(ctx, "NginxFileZipServlet", new NginxFileZipServlet());
        ctx.addServletMappingDecoded("/api/nginx/file/zip", "NginxFileZipServlet");

        tomcat.addServlet(ctx, "NginxFileUnZipServlet", new NginxFileUnZipServlet());
        ctx.addServletMappingDecoded("/api/nginx/file/unzip", "NginxFileUnZipServlet");

        tomcat.addServlet(ctx, "NginxGetProgressSerlvet", new NginxGetProgressSerlvet());
        ctx.addServletMappingDecoded("/api/nginx/file/progress", "NginxGetProgressSerlvet");
        ///endregion

        ///region firewall
        tomcat.addServlet(ctx, "FireWallListServlet", new FireWallListServlet());
        ctx.addServletMappingDecoded("/api/firewall/list", "FireWallListServlet");

        tomcat.addServlet(ctx, "FireWallInsertServlet", new FireWallInsertServlet());
        ctx.addServletMappingDecoded("/api/firewall/insert", "FireWallInsertServlet");

        tomcat.addServlet(ctx, "FireWallDeleteServlet", new FireWallDeleteServlet());
        ctx.addServletMappingDecoded("/api/firewall/delete", "FireWallDeleteServlet");
        ///endregion

        ///region command
        tomcat.addServlet(ctx, "CommandListServlet", new CommandListServlet());
        ctx.addServletMappingDecoded("/api/command/list", "CommandListServlet");

        tomcat.addServlet(ctx, "CommandDeleteServlet", new CommandDeleteServlet());
        ctx.addServletMappingDecoded("/api/command/delete", "CommandDeleteServlet");

        tomcat.addServlet(ctx, "CommandDetailServlet", new CommandDetailServlet());
        ctx.addServletMappingDecoded("/api/command/detail", "CommandDetailServlet");

        tomcat.addServlet(ctx, "CommandUpdateServlet", new CommandUpdateServlet());
        ctx.addServletMappingDecoded("/api/command/update", "CommandUpdateServlet");

        tomcat.addServlet(ctx, "CommandInsertServlet", new CommandInsertServlet());
        ctx.addServletMappingDecoded("/api/command/insert", "CommandInsertServlet");

        tomcat.addServlet(ctx, "CommandExecuteServlet", new CommandExecuteServlet());
        ctx.addServletMappingDecoded("/api/command/execute", "CommandExecuteServlet");
        ///endregion

        ///region group
        tomcat.addServlet(ctx, "GroupListServlet", new GroupListServlet());
        ctx.addServletMappingDecoded("/api/group/list", "GroupListServlet");

        tomcat.addServlet(ctx, "GroupDeleteServlet", new GroupDeleteServlet());
        ctx.addServletMappingDecoded("/api/group/delete", "GroupDeleteServlet");

        tomcat.addServlet(ctx, "GroupDetailServlet", new GroupDetailServlet());
        ctx.addServletMappingDecoded("/api/group/detail", "GroupDetailServlet");

        tomcat.addServlet(ctx, "GroupUpdateServlet", new GroupUpdateServlet());
        ctx.addServletMappingDecoded("/api/group/update", "GroupUpdateServlet");

        tomcat.addServlet(ctx, "GroupInsertServlet", new GroupInsertServlet());
        ctx.addServletMappingDecoded("/api/group/insert", "GroupInsertServlet");
        ///endregion

        ///region filemanage
        tomcat.addServlet(ctx, "FileListSerlvet", new FileListSerlvet());
        ctx.addServletMappingDecoded("/api/pfmanage/list", "FileListSerlvet");

        tomcat.addServlet(ctx, "FileDetailServlet", new FileDetailServlet());
        ctx.addServletMappingDecoded("/api/pfmanage/detail", "FileDetailServlet");

        tomcat.addServlet(ctx, "FileDownLoadServlet", new FileDownLoadServlet());
        ctx.addServletMappingDecoded("/api/pfmanage/download", "FileDownLoadServlet");
        ///endregion

        ///region version
        tomcat.addServlet(ctx, "VersionListServlet", new VersionListServlet());
        ctx.addServletMappingDecoded("/api/depolyversion/list", "VersionListServlet");

        tomcat.addServlet(ctx, "VersionDetailServlet", new VersionDetailServlet());
        ctx.addServletMappingDecoded("/api/depolyversion/detail", "VersionDetailServlet");

        tomcat.addServlet(ctx, "VersionUpdateServlet", new VersionUpdateServlet());
        ctx.addServletMappingDecoded("/api/depolyversion/update", "VersionUpdateServlet");

        tomcat.addServlet(ctx, "VersionDeleteServlet", new VersionDeleteServlet());
        ctx.addServletMappingDecoded("/api/depolyversion/delete", "VersionDeleteServlet");

        tomcat.addServlet(ctx, "VersionRecoverServlet", new VersionRecoverServlet());
        ctx.addServletMappingDecoded("/api/depolyversion/recover", "VersionRecoverServlet");
        ///endregion
    }

    public static void addProxy(ProxyDO entity) {
        String name = entity.getProxyID();
        ProxyServlet servlet = new ProxyServlet();
        Wrapper wrapper = tomcatInstance.addServlet(ctxInstance, name, servlet);
        wrapper.addInitParameter("targetUri", entity.getProxyTargetUrl());
        wrapper.addInitParameter("log", "false");
        ctxInstance.addServletMappingDecoded(entity.getProxyRoutePattern(), name);
    }

    public static void updateProxy(ProxyDO entity) {
        deleteProxy(entity);
        addProxy(entity);
    }

    public static void deleteProxy(ProxyDO entity) {
        String name = entity.getProxyID();
        ctxInstance.removeServletMapping(entity.getProxyRoutePattern());
        Container wrapper = ctxInstance.findChild(name);
        if (wrapper != null) {
            ctxInstance.removeChild(wrapper);
        }

    }
}
